package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.constant.UpmpConstant;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrganization;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpSysParam;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpOrganizationService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.RedpUpmpUtil;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.bizimpl.upmp.dao.AlipayInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.AlipayInfo;
import cc.rengu.redp.bizimpl.upmp.service.AlipayInfoService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author LiuYuLiang.LL
 */
@Service
@DS("upmp")
public class AlipayInfoServiceImpl extends ServiceImpl<AlipayInfoMapper, AlipayInfo> implements AlipayInfoService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public IPage<AlipayInfo> findByPage(QueryRequest request, AlipayInfo alipayInfo) {
        try {
            LambdaQueryWrapper<AlipayInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(alipayInfo.getAlipayNo())) {
                queryWrapper.like(AlipayInfo::getAlipayNo, alipayInfo.getAlipayNo());
            }
            if (StringUtils.isNotBlank(alipayInfo.getApFstName())) {
                queryWrapper.like(AlipayInfo::getApFstName, alipayInfo.getApFstName()).or()
                .like(AlipayInfo::getApSecName, alipayInfo.getApSecName()).or()
                .like(AlipayInfo::getApThiName, alipayInfo.getApThiName());
            }
            queryWrapper.orderByDesc(AlipayInfo::getCreateTime);
            Page<AlipayInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("获取支付宝类目信息失败", e);
            return null;
        }
    }

    @Override
    @Transactional
    public void createAlipayInfo(AlipayInfo alipayInfo) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user= CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        alipayInfo.setStatus(BusinessContainer.data_sta_01);
        alipayInfo.setAuditStatus(BusinessContainer.status_02);
        alipayInfo.setOprId(user.getId());
        alipayInfo.setCrtOrgId(user.getOrgCode());
        alipayInfo.setCreateTime(lastUpddt);
        alipayInfo.setLastOprId(user.getId());
        alipayInfo.setLastUpdateTime(lastUpddt);
        this.baseMapper.insert(alipayInfo);
    }

    @Override
    @Transactional
    public void updateAlipayInfo(AlipayInfo alipayInfo) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user= CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        // 修改
        alipayInfo.setAuditStatus(BusinessContainer.status_42);
        alipayInfo.setLastOprId(user.getId());
        alipayInfo.setLastUpdateTime(lastUpddt);
        this.baseMapper.updateById(alipayInfo);
    }

    @Override
    @Transactional
    public void cancelAlipayInfo(String id) {
        // 注销
        AlipayInfo entity = this.getById(id);
        entity.setStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void deleteAlipayInfo(String[] alipayInfos) {
        List<String> list = Arrays.asList(alipayInfos);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    public AlipayInfo getByUk(String alipayNo) {
        return this.baseMapper.getByUk(alipayNo);
    }

    // 查重方法1
    @Override
    public List<String> findAllAlipayNo() {
        return this.baseMapper.findAllAlipayNo();
    }

    // 查重方法2
    @Override
    public List<String> findAllAlipayNo(String alipayNo, String id) {
        return this.baseMapper.findAllAlipayNo(alipayNo, id);
    }

    // 查重方法3
    @Override
    public List<String> findAllAlipayName() {
        return this.baseMapper.findAllAlipayName();
    }

    // 查重方法4
    @Override
    public List<String> findAllAlipayName(String alipayName, String id) {
        return this.baseMapper.findAllAlipayName(alipayName, id);
    }
}
